package com.yx.fsysserver.file.controller;

import java.io.IOException;

import com.yx.fsysserver.file.component.WebSocketServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * WebSocket服务器端推送消息示例Controller
 * 群发example: http://localhost:88/demo/api/ws/sendAll?message=%E7%BE%A4%E5%8F%91%E6%B6%88%E6%81%AF%E5%86%85%E5%AE%B911
 * 单发example: http://localhost:88/demo/api/ws/sendOne?message=singlemessage&id=8
 *
 * @author wallimn，http://wallimn.iteye.com
 *
 */
@RestController
@RequestMapping("/api/ws")
public class WebSocketController {

    @RequestMapping(value="/sendAll", method=RequestMethod.GET)
    /**
     * 群发消息内容
     * @param message
     * @return
     */
    String sendAllMessage(@RequestParam(required=true) String message){
        try {
            WebSocketServer.BroadCastInfo(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }
    @RequestMapping(value="/sendOne", method=RequestMethod.GET)
    /**
     * 指定会话ID发消息
     * @param message 消息内容
     * @param id 连接会话ID
     * @return
     */
    String sendOneMessage(@RequestParam(required=true) String message,@RequestParam(required=true) String id){
        try {
            WebSocketServer.SendMessage(id,message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }
}
